<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\AdminLogin;
use app\common\model\AdminLoginLogModel;
use app\common\model\AdminLogModel;
use app\common\model\AdminMenuModel;
use app\common\model\AdminRoleAuthModel;
use app\common\model\AdminUserModel;
use app\common\model\AdminRoleModel;
use app\common\model\RegionListModel;
use app\common\model\SysConfModel;
use app\common\model\UploadListModel;
use app\common\service\AdminMenuService;
use app\common\service\RegionService;
use app\common\service\SysConfService;
use think\facade\Cache;
use think\facade\Validate;

class Admin extends AdminLogin
{

    protected $middleware = ['app\middleware\AdminLogin'];

    /*
     * @title 管理员列表
     * */
    public function user()
    {
        list($page, $limit) = $this->pageInfo();
        $map = [];
        //搜索条件
        $name = $this->request->param('name', '');
        if ($name) {
            $map[] = ['name', 'like', '%' . $name . '%'];
        }
        $phone = $this->request->param('phone', '');
        if ($phone) {
            $map[] = ['phone', '=', $phone];
        }
        $role_id = $this->request->param('role_id', '0');
        if ($role_id) {
            $map[] = ['role_id', '=', $role_id];
        }
        $status = $this->request->param('status', '0');
        if ($status) {
            $map[] = ['status', '=', $status];
        }
        $count = AdminUserModel::where($map)->count('uid');

        $export = $this->request->param('export', '0');
        if ($export) {
            if ($count > 5000) {
                return $this->error('一次最多导出5000条');
            }
            $list = AdminUserModel::where($map)->order('uid desc')->select()->toArray();
            $role_list = AdminRoleModel::column('role_id,name,introduce', 'role_id');
            $title = ['UID', '登录名', '用户名', '手机号', '角色', '邮箱', '错误次数', '单独登录', '当前状态', '添加时间'];
            $data = [];
            foreach ($list as $value) {
                $data[] = [
                    $value['uid'],
                    $value['username'],
                    $value['name'],
                    $value['phone'],
                    $role_list[$value['role_id']]['name'] ?? '未知',
                    $value['email'],
                    $value['error_num'],
                    $value['sso_status'] == 1 ? '开启' : '关闭',
                    $value['status'] == 1 ? '启用' : '禁用',
                    $value['add_time'],
                ];
            }
            return export_excel('用户列表', $title, $data);
        }
        $list = AdminUserModel::where($map)->limit($limit, $this->page_size)->order('uid desc')->select()->toArray();
        $page_info = $this->newPage($count, $this->page_size, $page);
        $role_list = AdminRoleModel::column('role_id,name,introduce', 'role_id');
        $this->assign('list', $list)
            ->assign('role_list', $role_list)
            ->assign('role_id', $role_id)
            ->assign('status', $status)
            ->assign('page_info', $page_info)
            ->assign('count', $count)
            ->assign('phone', $phone)
            ->assign('name', $name);
        return view();
    }

    /*
     * @title 管理员列表-添加
     * */
    public function userAdd()
    {
        if ($this->request->isPost()) {
            $Model = new AdminUserModel();
            $param = $this->request->param();
            //验证器验证
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = $Model->createPassword($param);
            if (!$result) {
                return $this->error($Model->error);
            }
            $result = AdminUserModel::create($param);
            if (!$result) {
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        } else {
            $role_list = AdminRoleModel::where('status', 1)->column('role_id,name,introduce', 'role_id');
            $this->assign('role_list', $role_list);
            return view();
        }
    }

    /*
     * @title 管理员列表-修改
     * */
    public function userEdit()
    {
        $Model = new AdminUserModel();
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //密码为空时不修改密码
            if (empty($param['password'])) {
                unset($Model->rule['password'], $Model->rule['password1'], $param['password'], $param['password1']);
            }
            //验证器验证
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }

            if (!empty($param['password'])) {
                $result = $Model->createPassword($param);
                if (!$result) {
                    return $this->error($Model->error);
                }
            }
            $param['error_num'] = 0;
            $result = AdminUserModel::update($param, ['uid' => $param['uid']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        } else {
            $uid = $this->request->param('uid', 0);
            if (!$uid) {
                return $this->error('参数错误');
            }
            $data = $Model::where('uid', $uid)->find();
            if (!$data) {
                return $this->error('用户没有找到');
            }
            $role_list = AdminRoleModel::where('status', 1)->column('role_id,name,introduce', 'role_id');
            $this->assign('role_list', $role_list)
                ->assign('data', $data->toArray());
            return view();
        }
    }

    /*
     * @title 管理员列表-用来批量修改使用
     * */
    public function userEditOp()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证器验证
            $validate = Validate::rule([
                'uid' => 'require',
                'status' => 'in:1,2',
                'sso_status' => 'in:1,2',
                'error_num' => 'between:0,10',
            ])->message([
                'uid.require' => '参数错误',
                'status.in' => '账号状态选择错误',
                'sso_status.in' => '单点登录状态选择错误',
                'error_num.between' => '错误次数范围0-10',
            ]);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = AdminUserModel::update($param, ['uid' => $param['uid']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        } else {
            $uid = $this->request->param('uid', '');
            if (empty($uid)) {
                return $this->error('参数错误');
            }
            $role_list = AdminRoleModel::where('status', 1)->column('role_id,name,introduce', 'role_id');
            $this->assign('role_list', $role_list)
                ->assign('uid', $uid);
            return view();
        }
    }

    /*
     * @title 管理员列表-删除
     * */
    public function userDel()
    {
        return $this->error('不允许删除,管理员包含日志等操作记录,请禁用该用户');
    }

    /*
     * @title 管理员列表-清除登录的错误次数
     * */
    public function userClearError()
    {
        if ($this->request->isPost()) {
            $uid = $this->request->param('uid', 0);
            AdminUserModel::update(['error_num' => 0], ['uid' => $uid]);
            return $this->success('处理成功');
        } else {
            return $this->error('错误的请求');
        }
    }

    /*
     * @title 管理员列表-设置用户的可用状态
     * */
    public function setUserStatus()
    {
        if ($this->request->isPost()) {
            $uid = $this->request->param('uid', 0);
            $status = $this->request->param('status', 0);
            $result = AdminUserModel::update(['status' => $status], ['uid' => $uid]);
            if (!$result) {
                return $this->error('更改失败');
            }
            return $this->success('更改成功');
        }
        return $this->error('错误的请求');
    }

    /*
     * @title 管理员列表-设置用户的单点登录状态
     * */
    public function setUserSso()
    {
        if ($this->request->isPost()) {
            $uid = $this->request->param('uid', 0);
            $status = $this->request->param('v', 0);
            $result = AdminUserModel::update(['sso_status' => $status], ['uid' => $uid]);
            if (!$result) {
                return $this->error('更改失败');
            }
            return $this->success('更改成功');
        }
        return $this->error('错误的请求');
    }

    /*
     * @title 公共方法-信息查看和登录密码的修改
     * */
    public function userInfo()
    {
        if ($this->request->isPost()) {
            $Model = new AdminUserModel();
            $param = $this->request->param();
            $rule = [];
            $rule['password'] = $Model->rule['password'];
            $rule['password1'] = $Model->rule['password1'];
            //验证器验证
            $validate = Validate::rule($rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = $Model->createPassword($param);
            if (!$result) {
                return $this->error($Model->error);
            }
            $result = AdminUserModel::update(['password' => $param['password'], 'salt' => $param['salt']], ['uid' => $this->request->user_info['uid']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        } else {
            $this->assign('user_info', $this->request->user_info);
            return view();
        }
    }

    /*
     * @title 菜单列表
     * */
    public function menu()
    {
        $Model = new AdminMenuModel();
        $this->assign('type', $Model->type)
            ->assign('list', AdminMenuService::ins()->_getAllMenu());
        return view();
    }

    /*
     * @title 菜单列表 - 添加
     * */
    public function menuAdd()
    {
        if ($this->request->isPost()) {
            $Model = new AdminMenuModel();
            $param = $this->request->param();
            //验证器验证
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            if ($param['pid'] != 0) {
                $parentData = AdminMenuModel::where('id', $param['pid'])->find();
                if (!$parentData) {
                    return $this->error('父菜单不存在');
                }
            }
            if (empty($param['icon'])) {
                $param['icon'] = $Model->icon;
            }
            $param['url'] = strtolower($param['url']);
            $result = AdminMenuModel::create($param);
            if (!$result) {
                return $this->error('添加失败');
            }
            //清除缓存
            AdminMenuService::ins()->clearCache();
            return $this->success('添加成功');
        } else {
            $pid = $this->request->param('pid', '0');
            $this->assign('pid', $pid);
            $this->assign('list', AdminMenuService::ins()->_getAllMenu(1));
            return view();
        }
    }

    /*
     * @title 菜单列表 - 修改
     * */
    public function menuEdit()
    {
        $Model = new AdminMenuModel();
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证器验证
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            if ($param['pid'] != 0) {
                $parentData = AdminMenuModel::where('id', $param['pid'])->find();
                if (!$parentData) {
                    return $this->error('父菜单不存在');
                }
            }
            if (empty($param['icon'])) {
                $param['icon'] = $Model->icon;
            }
            $param['url'] = strtolower($param['url']);
            $result = AdminMenuModel::update($param, ['id' => $param['id']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            //清除缓存
            AdminMenuService::ins()->clearCache();
            return $this->success('修改成功');
        } else {
            $id = $this->request->param('id', '0');
            $data = AdminMenuModel::where('id', $id)->find();
            if (!$data) {
                return $this->error('参数错误');
            }
            $this->assign('list', (AdminMenuService::ins())->_getAllMenu(1))->assign('data', $data);
            return view();
        }
    }

    /*
     * @title 菜单列表 - 删除
     * */
    public function menuDel()
    {
        if (!$this->request->isPost()) $this->error('错误的请求');
        $id = $this->request->param('id', 0);
        $result = AdminMenuModel::where('pid', $id)->find();
        if ($result) {
            return $this->error('请先删除子菜单');
        }
        AdminMenuModel::update(['delete_time' => time()], ['id' => $id]);
        AdminMenuService::ins()->clearCache();
        return $this->success('删除成功');
    }

    /*
     * @title 角色列表
     * */
    public function role()
    {
        list($page, $limit) = $this->pageInfo();
        $map = [];
        //搜索条件
        $name = $this->request->param('name', '');
        if ($name) {
            $map[] = ['name', 'like', '%' . $name . '%'];
        }
        $count = AdminRoleModel::where($map)->count('role_id');
        $list = AdminRoleModel::where($map)->limit($limit, $this->page_size)->select()->toArray();
        $page_info = $this->newPage($count, $this->page_size, $page);
        $this->assign('list', $list)
            ->assign('page_info', $page_info)
            ->assign('count', $count)
            ->assign('name', $name);
        return view();

    }

    /*
     * @title 角色列表 - 添加
     * */
    public function roleAdd()
    {
        if ($this->request->isPost()) {
            $Model = new AdminRoleModel();
            $param = $this->request->param();
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = AdminRoleModel::create($param);
            if (!$result) {
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        } else {
            return view();
        }
    }

    /*
     * @title 角色列表 - 修改
     * */
    public function roleEdit()
    {
        if ($this->request->isPost()) {
            $Model = new AdminRoleModel();
            $param = $this->request->param();
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = AdminRoleModel::update($param, ['role_id' => $param['role_id']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        } else {
            $role_id = $this->request->param('role_id');
            $data = AdminRoleModel::where('role_id', $role_id)->find();
            if (!$data) {
                return $this->error('参数错误');
            }
            $this->assign('data', $data);
            return view();
        }
    }

    /*
     * @title 角色列表 - 删除
     * */
    public function roleDel()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', '0');
            $user = AdminUserModel::where('role_id', $id)->find();
            if ($user) {
                return $this->error('请先删除对应管理员账号');
            }
            $result = AdminRoleModel::where('role_id', $id)->delete();
            if (!$result) {
                return $this->error('删除失败,信息可能已删除');
            }
            AdminRoleAuthModel::where('role_id', $id)->delete(); //删除角色对应的权限
            return $this->success('删除成功');
        }
        return $this->error('错误的请求');
    }

    /*
     * @title 角色列表 - 清除缓存
     * */
    public function clearRoleCache()
    {
        if ($this->request->isPost()) {
            $result = AdminMenuService::ins()->clearCache();
            if (!$result) {
                return $this->error('清除缓存失败');
            }
            return $this->success('清除缓存成功');
        }
        return $this->error('错误的请求');
    }

    /*
     * @title 角色列表 - 设置状态
     * */
    public function setRoleStatus()
    {
        if ($this->request->isPost()) {
            $role_id = $this->request->param('role_id', 0);
            $status = $this->request->param('status', 0);
            $result = AdminRoleModel::update(['status' => $status], ['role_id' => $role_id]);
            if (!$result) {
                return $this->error('更改失败');
            }
            return $this->success('更改成功');
        } else {
            return $this->error('错误的请求');
        }
    }

    /*
     * @title 角色列表 - 菜单授权
     * */
    public function addRule()
    {
        if ($this->request->isPost()) {
            $AdminRoleAuthModel = new AdminRoleAuthModel();
            $role_id = $this->request->param('role_id');
            $data = AdminRoleModel::where('role_id', $role_id)->find();
            if (!$data) {
                return $this->error('参数错误');
            }
            $delResult = AdminRoleAuthModel::where('role_id', $role_id)->delete();
            if ($delResult === false) {
                return $this->error('系统错误,用户权限更改失败');
            }
            $param = $this->request->param();
            $saveData = [];
            if (!empty($param['pid'])) {
                foreach ($param['pid'] as $key => $value) {
                    $saveData[] = ['role_id' => $role_id, 'menu_id' => $value];
                }
            }
            if (!empty($param['id'])) {
                foreach ($param['id'] as $k => $v) {
                    $saveData[] = ['role_id' => $role_id, 'menu_id' => $v];
                }
            }
            $result = $AdminRoleAuthModel->saveAll($saveData);
            if (!$result) {
                return $this->error('更新失败');
            }
            AdminMenuService::ins()->clearCache();
            return $this->success('更新成功');
        } else {
            $role_id = $this->request->param('role_id');
            $data = AdminRoleModel::where('role_id', $role_id)->find();
            if (!$data) {
                return $this->error('参数错误');
            }
            $list_auth = AdminRoleAuthModel::where('role_id', '=', $role_id)->column('menu_id');
            $this->assign('list', (new AdminMenuModel())->getMenu())
                ->assign('data', $data)
                ->assign('list_auth', $list_auth);
            return view();
        }
    }

    /*
     * @title 系统访问日志
     * */
    public function sysLog()
    {
        list($page, $limit) = $this->pageInfo();
        $map = [];
        //搜索条件
        $name = $this->request->param('name', '');
        if ($name) {
            $user_list = AdminUserModel::where('name', 'like', '%' . $name . '%')->select()->toArray();
            $map[] = ['uid', 'in', array_column($user_list, 'uid')];
        }
        $url = $this->request->param('url', '');
        if ($url) {
            $map[] = ['url', '=', $url];
        }
        $req_type = $this->request->param('req_type', '');
        if ($req_type) {
            $map[] = ['req_type', '=', $req_type];
        }
        $count = AdminLogModel::where($map)->count('id');
        $list = AdminLogModel::where($map)->limit($limit, $this->page_size)->order('id desc')->select()->toArray();
        $page_info = $this->newPage($count, $this->page_size, $page);

        $admin_list = [];
        if (!empty($list)) {
            $admin_list = AdminUserModel::where('uid', 'in', array_column($list, 'uid'))->column('uid,name', 'uid');
        }
        $this->assign('list', $list)
            ->assign('page_info', $page_info)
            ->assign('admin_list', $admin_list)
            ->assign('name', $name)
            ->assign('req_type', $req_type)
            ->assign('url', $url)
            ->assign('count', $count);
        return view();
    }

    /*
     * @title 系统配置列表
     * */
    public function sysConf()
    {
        $list = SysConfModel::select();
        $this->assign('list', $list)
            ->assign('type_list', (new SysConfModel())->type_list);
        return view();
    }

    /*
     * @title 系统配置列表 - 添加
     * */
    public function sysConfAdd()
    {
        if ($this->request->isPost()) {
            $Model = new SysConfModel();
            $param = $this->request->param();
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            if (!$Model->checkType($param)) {
                return $this->error($Model->error);
            }
            $param['key'] = strtoupper($param['key']);
            $result = SysConfModel::create($param);
            if (!$result) {
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        } else {
            $this->assign('type_list', (new SysConfModel())->type_list);
            return view();
        }
    }

    /*
     * @title 系统配置列表 - 修改
     * */
    public function sysConfEdit()
    {
        $Model = new SysConfModel();
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            if (!$Model->checkType($param)) {
                return $this->error($Model->error);
            }
            $result = SysConfModel::update($param, ['id' => $param['id']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        } else {
            $id = $this->request->param('id', 0);
            if (!$id) {
                return $this->error('参数错误');
            }
            $data = SysConfModel::where('id', $id)->find();
            if (!$data) {
                return $this->error('参数错误');
            }
            $this->assign('data', $data)
                ->assign('type_list', (new SysConfModel())->type_list);
            return view();
        }
    }

    /*
     * @title 系统配置列表 - 更新缓存(全部和单个)
     * */
    public function sysConfCache()
    {
        $id = $this->request->param('id', '');
        $SysConfService = new SysConfService();
        if (empty($id)) { //更新全部缓存
            $list = SysConfModel::select();
            foreach ($list as $value) {
                $SysConfService->setKey($value->getAttr('key'), $value->getAttr('value'), $value->getAttr('type'));
            }
        } else { //更新单个缓存
            $result = SysConfModel::where('id', $id)->find();
            if (!$result) {
                $this->error('没有找到该配置');
            }
            $SysConfService->setKey($result->getAttr('key'), $result->getAttr('value'), $result->getAttr('type'));
        }
        return $this->success('更新成功');
    }

    /*
     * @title 登录日志
     * */
    public function loginLog()
    {
        list($page, $limit) = $this->pageInfo();
        $map = [];
        //搜索条件
        $name = $this->request->param('name', '');
        if ($name) {
            $user_list = AdminUserModel::where('name', 'like', '%' . $name . '%')->select()->toArray();
            $map[] = ['uid', 'in', array_column($user_list, 'uid')];
        }
        $count = AdminLoginLogModel::where($map)->count('id');
        $list = AdminLoginLogModel::where($map)->limit($limit, $this->page_size)->order('id desc')->select()->toArray();
        $page_info = $this->newPage($count, $this->page_size, $page);

        foreach ($list as &$value) {
            $value['password'] = hide_info($value['password']);
        }
        $admin_list = AdminUserModel::where('uid', 'in', array_column($list, 'uid'))->column('uid,name', 'uid');

        $this->assign('list', $list)
            ->assign('page_info', $page_info)
            ->assign('admin_list', $admin_list)
            ->assign('name', $name)
            ->assign('count', $count);
        return view();
    }

    /*
     * @title 更新全站静态资源缓存
     * */
    public function clearStatic()
    {
        if (!$this->request->isPost()) {
            return $this->error('错误的请求');
        }
        $key = config('app.sys_static_key');
        $result = Cache::store('redis')->set($key, uniqid());
        if (!$result) {
            return $this->error('更新失败');
        }
        return $this->success('更新成功');
    }

    /*
     * @title 上传的文件列表
     * */
    public function uploadList()
    {
        list($page, $limit) = $this->pageInfo();
        $map = [];
        //搜索条件
        $name = $this->request->param('name', '');
        if ($name) {
            $user_list = AdminUserModel::where('name', 'like', '%' . $name . '%')->select()->toArray();
            $map[] = ['uid', 'in', array_column($user_list, 'uid')];
        }
        $count = UploadListModel::where($map)->count('id');
        $list = UploadListModel::where($map)->limit($limit, $this->page_size)->order('id desc')->select()->toArray();
        $page_info = $this->newPage($count, $this->page_size, $page);
        $admin_list = AdminUserModel::where([['uid', 'in', array_column($list, 'uid')]])->column('uid,name');
        $UploadListModel = new UploadListModel();
        $this->assign('list', $list)
            ->assign('page_info', $page_info)
            ->assign('admin_list', $admin_list)
            ->assign('from_list', $UploadListModel->from_list)
            ->assign('name', $name)
            ->assign('count', $count);
        return view();
    }

    /*
     * @title 添加一个上传日志
     * */
    public function uploadLog()
    {
        if (!$this->request->isPost()) {
            return $this->error('错误的请求');
        }
        $param = $this->request->param();
        if (empty($param['url'])) {
            return $this->error('参数错误');
        }
        $result = (new UploadListModel())->addLog($this->request->user_info['uid'], $param['url'], 1, $param['introduce']);
        if (!$result) {
            return $this->error('添加失败');
        }
        return $this->success('添加成功');
    }

    /*
     * @title 区域字典 - 列表
     * */
    public function regionList()
    {
        list($page, $limit) = $this->pageInfo();
        $map = [];
        $area = $this->areaInfo();//地区搜索
        if ($area) {
            $map[] = ['region_code', 'like', $area . '%'];
        }
        $count = RegionListModel::where($map)->count('region_code');
        $list = RegionListModel::where($map)->limit($limit, $this->page_size)->select()->toArray();
        $page_info = $this->newPage($count, $this->page_size, $page);
        $this->assign('list', $list)
            ->assign('page_info', $page_info)
            ->assign('count', $count);
        return view();
    }

    /*
     * @title 添加
     * */
    public function regionAdd()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $Model = new RegionListModel();
            //验证器验证
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = RegionListModel::create($param);
            if (!$result) {
                return $this->error('添加失败');
            }
            (new RegionService())->setRedisCache(false);
            return $this->success('添加成功');
        } else {
            $RegionService = new RegionService();
            $this->assign('list', $RegionService->getAllLev());
            return view();
        }
    }

    /*
     * @title 修改
     * */
    public function regionEdit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $Model = new RegionListModel();
            //验证器验证
            $validate = Validate::rule($Model->rule)->message($Model->message);
            if (!$validate->check($param)) {
                return $this->error($validate->getError());
            }
            $result = RegionListModel::update($param, ['region_code' => $param['region_code']]);
            if (!$result) {
                return $this->error('修改失败');
            }
            (new RegionService())->setRedisCache(false);
            return $this->success('修改成功');
        } else {
            $region_code = $this->request->get('region_code', '');
            $data = RegionListModel::where('region_code', $region_code)->find();
            if (!$data) {
                return $this->error('参数错误');
            }
            $RegionService = new RegionService();
            $this->assign('list', $RegionService->getAllLev())->assign('data', $data);
            return view();
        }
    }

    /*
     * @title 删除
     * */
    public function regionDel()
    {
        if (!$this->request->isPost()) $this->error('错误的请求');
        $region_code = $this->request->param('id', 0);
        $result = RegionListModel::where('pid', $region_code)->find();
        if ($result) {
            return $this->error('请先删除子区域');
        }
        RegionListModel::where('region_code', $region_code)->delete();
        (new RegionService())->setRedisCache(false);
        return $this->success('删除成功');
    }

    /*
     * @title 区域字典 - 添加示例
     * */
    public function regionAddExample()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $RegionService = new RegionService();
            $code = $RegionService->getAreaCode($param);
            $name = $RegionService->getAreaName($code);
            return $this->success('选择的地区编码:' . $code . ',地区名称:' . $name);
        } else {
            return view();
        }
    }

    /*
     * @title 区域字典 - 修改示例
     * */
    public function regionEditExample()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $RegionService = new RegionService();
            $code = $RegionService->getAreaCode($param);
            $name = $RegionService->getAreaName($code);
            return $this->success('选择的地区编码:' . $code . ',地区名称:' . $name);
        } else {
            $region_code = $this->request->param('region_code', '');
            $this->assign('area_info', (new RegionService())->getAreaInfo($region_code));
            return view();
        }
    }

    /*
     * @title 区域字典 - 更新缓存
     * */
    public function setRegionCache()
    {
        if (!$this->request->isPost()) {
            return $this->error('错误的请求');
        }
        (new RegionService())->setRedisCache();
        return $this->success('缓存写入成功');
    }

}